package org.example.gxpt.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.gxpt.common.constant.LoginRoleConstant;
import org.example.gxpt.common.context.BaseContext;
import org.example.gxpt.common.context.LoginContext;
import org.example.gxpt.mapper.InstrumentAdminMapper;
import org.example.gxpt.mapper.ShowroomAdminMapper;
import org.example.gxpt.mapper.SystemAdminMapper;
import org.example.gxpt.mapper.UserMapper;
import org.example.gxpt.pojo.entity.InstrumentAdmin;
import org.example.gxpt.pojo.entity.ShowroomAdmin;
import org.example.gxpt.pojo.entity.SystemAdmin;
import org.example.gxpt.pojo.entity.User;
import org.example.gxpt.pojo.userdetails.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private InstrumentAdminMapper instrumentAdminMapper;
    @Autowired
    private ShowroomAdminMapper showroomAdminMapper;
    @Autowired
    private SystemAdminMapper systemAdminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        log.info("取出存储在LoginContext中的值:{}", LoginContext.getCurrentRole().toString());

        //由于所有角色的用户在使用SpringSecurity登录时都需要走这里，所以需要根据在Service中存储的Login上下文来判断是哪个用户在登录
        //User登陆逻辑
        if (LoginContext.getCurrentRole().equals(LoginRoleConstant.USER)){
            //当前为user登录，所以在UserMapper中查询用户信息，并返回由User封装的UserDetails对象
            log.info("当前为user登录");
            //根据用户名查询用户信息
            User user = userMapper.getUserByUsername(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(user)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(user);
        }//InstrumentAdmin登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.INSTRUMENT_ADMIN)){
            //当前为instrumentAdmin登录，所以在instrumentAdminMapper中查询用户信息，并返回由InstrumentAdmin封装的UserDetails对象
            log.info("当前为InstrumentAdmin登录");
            //根据用户名查询用户信息
            InstrumentAdmin instrumentAdmin = instrumentAdminMapper.selectByUsername(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(instrumentAdmin)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(instrumentAdmin);
        }//ShowroomAdmin登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.SHOWROOM_ADMIN)){
            //当前为showroomAdmin登录，所以在showroomAdminMapper中查询用户信息，并返回由ShowroomAdmin封装的UserDetails对象
            log.info("当前为ShowroomAdmin登录");
            //根据用户名查询用户信息
            ShowroomAdmin showroomAdmin = showroomAdminMapper.selectByUsername(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(showroomAdmin)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(showroomAdmin);
        }//SystemAdmin登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.SYSTEM_ADMIN)){
            //当前为systemAdmin登录，所以在systemAdminMapper中查询用户信息，并返回由SystemAdmin封装的UserDetails对象
            log.info("当前为SystemAdmin登录");
            //根据用户名查询用户信息
            SystemAdmin systemAdmin = systemAdminMapper.selectByUsername(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(systemAdmin)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(systemAdmin);
        }//User电话登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.USER_PHONE)){
            //当前为user登录，所以在UserMapper中查询用户信息，并返回由User封装的UserDetails对象
            log.info("当前为user手机登录");
            //根据用户名查询用户信息
            User user = userMapper.getUserByPhone(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(user)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(user);
        }//InstrumentAdmin电话登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.INSTRUMENT_ADMIN_PHONE)){
            //当前为instrumentAdmin登录，所以在instrumentAdminMapper中查询用户信息，并返回由InstrumentAdmin封装的UserDetails对象
            log.info("当前为InstrumentAdmin手机登录");
            //根据用户名查询用户信息
            InstrumentAdmin instrumentAdmin = instrumentAdminMapper.selectByPhone(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(instrumentAdmin)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(instrumentAdmin);
        }//ShowroomAdmin登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.SHOWROOM_ADMIN_PHONE)){
            //当前为showroomAdmin登录，所以在showroomAdminMapper中查询用户信息，并返回由ShowroomAdmin封装的UserDetails对象
            log.info("当前为ShowroomAdmin手机登录");
            //根据用户名查询用户信息
            ShowroomAdmin showroomAdmin = showroomAdminMapper.selectByPhone(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(showroomAdmin)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(showroomAdmin);
        }//SystemAdmin登陆逻辑
        else if (LoginContext.getCurrentRole().equals(LoginRoleConstant.SYSTEM_ADMIN_PHONE)){
            //当前为systemAdmin登录，所以在systemAdminMapper中查询用户信息，并返回由SystemAdmin封装的UserDetails对象
            log.info("当前为SystemAdmin手机登录");
            //根据用户名查询用户信息
            SystemAdmin systemAdmin = systemAdminMapper.selectByPhone(username);
            //如果查询不到数据就通过抛出异常来给出提示
            if (Objects.isNull(systemAdmin)) {
                throw new RuntimeException("用户名或密码错误");
            }
            //封装成UserDetails对象返回,loginuser根据其中的对象自动判断权限
            return new LoginUser(systemAdmin);
        }

        return null;
    }
}
